import {createStore,combineReducers,applyMiddleware} from 'redux'
import reduxThunk from 'redux-thunk'
import * as types from './types'
//combineReducers  合并reducer的
// action 其实就是一个对象（{type:"",qqq}）
let countReducer = (state,action)=>{
  console.log('countReducer',action.type)
  state = state || {
    num:100,
    color:'red'
  }
  switch (action.type) {
    case types.ADD:
      return {
        ...state,
        num:state.num + action.qqq
      }
    case types.MINUS:{
      return {
        ...state,
        num:state.num - action.num
      }
    }   
    default:
      return {
        ...state
      }
  }
}

let todoReducer = (state,action)=>{
  state = state || {
    todoList:[{id:23424,str:"吃饭"}]
  }
  // console.log('todoReducer',action.type)
  switch (action.type) {
    case types.ADDTODO:
      return {
        todoList:state.todoList.concat(action.option)
      }
    case types.MINUSTODO :
      let ary = state.todoList.filter((item)=>item.id!==action.id)
      return{
        todoList:ary
      } 
  
    default:
      return {
        ...state
      }
  }
}

let rootReducer = combineReducers({
  myCount:countReducer,
  myTodo:todoReducer
})
let store = createStore(rootReducer,applyMiddleware(reduxThunk))


export default store